package xyz.robinbingo.rbtask.common.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import xyz.robinbingo.rbtask.common.enums.ResEnum;

import java.io.Serial;
import java.io.Serializable;

/**
 * BaseRes
 *
 * @author Robin
 * @date 2020/9/2 14:59
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -1071775210256167364L;

    private Integer code;
    private String message;
    private T data;

    public ResResult(ResEnum resEnum) {
        this.code = resEnum.getCode();
        this.message = resEnum.getMessage();
    }

    public static <T> ResResult<T> success() {
        return new ResResult<>(ResEnum.SUCCESS);
    }

    public static <T> ResResult<T> success(T data) {
        ResResult<T> res = new ResResult<>(ResEnum.SUCCESS);
        res.setData(data);
        return res;
    }

    public static <T> ResResult<T> fail(ResEnum resEnum) {
        return new ResResult<>(resEnum);
    }

    public static <T> ResResult<T> fail(Integer code, String message) {
        return new ResResult<>(code, message, null);
    }

    public static <T> ResResult<T> fail(String message) {
        return new ResResult<>(ResEnum.BUSINESS_ERROR.getCode(), message, null);
    }
}
